package others;

/**
 * @author pengfei.hpf
 * @date 2020/3/8
 * @verdion 1.0.0
 */
public class CheckValidString {
    public boolean checkValidString(String s) {
        if(s == null || s.length() == 0){
            return true;
        }
        char[] chars = s.toCharArray();
        int leftCount = 0;
        int rightUsable = 0;
        for(int i = 0; i < chars.length; i ++){
            char c = chars[i];
            if(c == '('){
                leftCount ++;
                rightUsable ++;
            } else if(c == ')'){
                if(leftCount > 0){
                    leftCount --;
                }
                if( rightUsable > 0){
                    rightUsable --;
                } else {
                    return false;
                }
            } else if(c == '*'){
                if(leftCount > 0 ){
                    leftCount --;
                }
                rightUsable++;
            }
        }

        return leftCount <= 0 && rightUsable >= 0;
    }
}
